package cemetery.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import cemetery.bean.account;
import cemetery.service.AccountService;
import cemetery.service.impl.AccountServiceImpl;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		        //获取请求中的数据
				//解决中文乱码
				response.setContentType("text/json;charset=utf-8");
				String action=request.getParameter("action");
				
				
				
				
				if("select".equals(action)) {
					String user=request.getParameter("user");
					String pwd=request.getParameter("pwd");					
					account a=new account();
					a.setUser(user);
					a.setPwd(pwd);			    
					AccountService ac=new AccountServiceImpl();
					//调用业务逻辑层的代码
				    int count=	ac.denglu(a);
					//准备数据,进行页面跳转
				    Map<String, Object> map = new HashMap<String, Object>();
					if (count> 0) {						
						map.put("msg", "成功");
						map.put("count", 1);
						request.getSession().setAttribute("user", user);
					} else {					
						map.put("msg", "用户名或密码错误");
						map.put("count", -1);
					}
					PrintWriter pw = response.getWriter();
					String result = JSON.toJSONString(map);
					pw.write(result);
					pw.flush();// 刷新
					pw.close();
				}
				
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
